//
//  HJAuthView.m
//  project
//
//  Created by jc on 2019/4/13.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJAuthView.h"

@interface HJAuthView ()
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *cardNumTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneNum;
@property (weak, nonatomic) IBOutlet UITextField *smsCodeTF;
@property (weak, nonatomic) IBOutlet UIButton *smsBtn;
@property (weak, nonatomic) IBOutlet UIButton *authBtn;
@property (weak, nonatomic) IBOutlet UIView *namView;
@property (weak, nonatomic) IBOutlet UIView *cardView;
@property (weak, nonatomic) IBOutlet UIView *phoneView;
@property (weak, nonatomic) IBOutlet UIView *smsView;

@end

@implementation HJAuthView

-(instancetype)init {
    if (self == [super init]) {
        self = [[NSBundle mainBundle]loadNibNamed:@"HJAuthView" owner:nil options:nil].firstObject;
        [self layerConerRadius];
        [self textRACSignal];
        [self configBtnSubmit];
        [self configBtnAuth];
    }
    return self;
}
-(void)layerConerRadius {
    [self.smsBtn rounded:UIRectCornerAllCorners radius:6];
    [self.authBtn rounded:UIRectCornerAllCorners radius:6];
    
    [self setupShadowWithView:self.namView];
    [self setupShadowWithView:self.cardView];
    [self setupShadowWithView:self.phoneView];
    [self setupShadowWithView:self.smsView];
}

- (IBAction)smsBtnClicked:(UIButton *)sender {
    [sender creatTimer];
    NSString * smsCodeUrl = [Api stringByAppendingString:AuthSmsApi];
    [HJHttpTool post:smsCodeUrl params:@{@"mobile":self.phoneNum.text} success:^(HJResponeds *result) {
        LKLog(@"发送验证码:%d",result.isSuccess);
        [[UIViewController currentViewController] showSuccessHUDWithStr:@"发送成功"];
    } failure:^(id result) {
        [[UIViewController currentViewController] showErrorHUDWithError:result];
    }];
}

-(void)textRACSignal {
    @weakify(self)
    [[self.nameTF.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
        return value.length>=8;
    }] subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        self.nameTF.text = [x substringToIndex:8];
    }];
    
    [[self.phoneNum.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
        return value.length >=11;
    }]subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        self.phoneNum.text = [x substringToIndex:11];
    }];
//    [[self.cardNumTF.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
//        return value.length >=
//    }]subscribeNext:^(NSString * _Nullable x) {
//        @strongify(self
//    }];
    [[self.smsCodeTF.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
        return value.length >=6;
    }]subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        self.smsCodeTF.text = [x substringToIndex:6];
    }];
}

#pragma mark --创建RAC响应登录按钮是否可以点击
- (void)configBtnSubmit {
    @weakify(self)
    RAC(self.smsBtn,userInteractionEnabled) = [RACSignal combineLatest:@[self.nameTF.rac_textSignal,self.cardNumTF.rac_textSignal,self.phoneNum.rac_textSignal] reduce:^id (NSString * name, NSString * cardNum, NSString *phone) {
        @strongify(self)
        if (phone.length ==11 && [CommonTools validateIdentityCard:cardNum] && name.length >=1) {
            self.smsBtn.backgroundColor = [UIColor blackColor];
            return @YES;
        }
        self.smsBtn.backgroundColor = [UIColor lightGrayColor];
        return @NO;
    }];
}

-(void)configBtnAuth {
    @weakify(self)
    RAC(self.authBtn,userInteractionEnabled) = [RACSignal combineLatest:@[self.nameTF.rac_textSignal,self.cardNumTF.rac_textSignal,self.phoneNum.rac_textSignal,self.smsCodeTF.rac_textSignal] reduce:^id (NSString * name, NSString * cardNum, NSString * phone, NSString * smsCode) {
        @strongify(self)
        if (phone.length ==11 && smsCode.length ==6 && [CommonTools validateIdentityCard:cardNum] && name.length >=1) {
            self.authBtn.backgroundColor = [UIColor blackColor];
            return @YES;
        }
        self.authBtn.backgroundColor = [UIColor lightGrayColor];
        return @NO;
    }];
}
- (IBAction)authBtnClicked:(id)sender {
    NSMutableDictionary * parmas = [NSMutableDictionary dictionary];
    parmas[@"name"] = self.nameTF.text;
    parmas[@"cardCode"] = self.cardNumTF.text;
    parmas[@"mobile"] = self.phoneNum.text;
    parmas[@"userId"] = [HJUserManager getUserModel].userId;
    parmas[@"code"] = self.smsCodeTF.text;
    [self.delegate authBtnClickedWithParams:parmas];
}
@end
